//
//  WZMineCell.m
//  ETong
//
//  Created by wanzhao on 15/10/29.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZMineCell.h"
#import "WZMineItem.h"


@interface WZMineCell ()

@property (nonatomic, weak) UIImageView *headerImage;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UIView *backView;

@end

@implementation WZMineCell

+ (instancetype)mineCellWithTableView:(UITableView *)tableView
{
    static NSString *mineId = @"mineCell";
    WZMineCell *cell = [tableView dequeueReusableCellWithIdentifier:mineId];
    if (cell == nil) {
        cell = [[WZMineCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:mineId];
    }
    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = WhiteColor;
        
        UIView *backView = [[UIView alloc] init];

        backView.backgroundColor = [UIColor clearColor];

        [self.contentView addSubview:backView];
        backView.hidden = YES;
        self.backView = backView;

        
//        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        UIButton *acVIew = [UIButton buttonWithType:UIButtonTypeCustom];
        acVIew.userInteractionEnabled = NO;
        [acVIew setImage:[UIImage imageNamed:@"cellArrow"] forState:UIControlStateNormal];
        acVIew.width = acVIew.currentImage.size.width;
        self.accessoryView = acVIew;
        
        // 1.设置标题的字体大小
        self.textLabel.font = Font17;
        self.textLabel.textColor = RGBCOLORWITH16COLOR(0x333333);
        self.textLabel.backgroundColor = ClearColor;
        self.textLabel.numberOfLines = 1;
        self.detailTextLabel.textColor = RGBCOLORWITH16COLOR(0x666666);
        self.detailTextLabel.font = Font12;
        self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        self.detailTextLabel.backgroundColor = ClearColor;
        self.detailTextLabel.numberOfLines = 2;
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = RGBCOLORWITH16COLOR(0xcccccc);
        [self.contentView addSubview:line];
        self.line = line;
       
        
        
        //创建头像
        UIImageView *headerImage = [[UIImageView alloc] init];
        headerImage.hidden = YES;
        headerImage.layer.cornerRadius = 8.0f;
        headerImage.layer.masksToBounds = YES;
        headerImage.image = [UIImage imageNamed:@"personal_title"];
        [self.contentView addSubview:headerImage];
        self.headerImage = headerImage;
        
        //创建用户名以及用户信息label
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.hidden = YES;
        self.nameLabel = nameLabel;
        [self.contentView addSubview:nameLabel];
        nameLabel.numberOfLines = 0;
    }
    return self;
}

- (void)updateUserTouxiang
{
    if ([self.delegate respondsToSelector:@selector(mineCellDidClickTouxiang)]) {
        [self.delegate mineCellDidClickTouxiang];
    }
}

- (void)setItem:(WZMineItem *)item
{
    if (item.isHeadImage) {
        self.headerImage.hidden = NO;
        self.nameLabel.hidden = NO;
        NSMutableAttributedString *attr = [NSMutableAttributedString attributeVenueStrWithText:item.title text2:item.subTitle color1:RGBCOLORWITH16COLOR(0x333333) color2:RGBCOLORWITH16COLOR(0x666666) font1:Font17 font2:Font12];
        self.nameLabel.attributedText = attr;

    } else {
        self.headerImage.hidden = YES;
        self.nameLabel.hidden = YES;
        self.textLabel.text = item.title;
        self.detailTextLabel.text = item.subTitle;
        if (item.icon.length > 0) {
            
            self.imageView.image = [UIImage imageNamed:item.icon];
            self.imageView.hidden = NO;
        } else {
            self.imageView.hidden = YES;
        }

    }
    self.accessoryView.hidden = item.isHiddenAccessView;
    self.backView.hidden = item.isHiddenBackView;
    self.userInteractionEnabled = item.isHiddenBackView;
}

- (void)setCellIndexPath:(NSIndexPath *)cellIndexPath
{
    _cellIndexPath = cellIndexPath;
    
}

//-(void)setDetail:(NSString *)detailTitle
//{
//    self.detailTextLabel.text = detailTitle;
//    NSLog(@"%@",)
//}
//

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.line.frame = CGRectMake(OppositeWidthRate(10), self.height - 0.5, self.width - OppositeWidthRate(10), 0.5);
    self.accessoryView.x = self.width - OppositeWidthRate(10) - self.accessoryView.width;
    self.accessoryView.y = 0;
    self.accessoryView.height = self.height;
    
    self.headerImage.frame = CGRectMake(OppositeWidthRate(15), OppositeHeightRate(12), self.height - OppositeHeightRate(24), self.height - OppositeHeightRate(24));
    self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.headerImage.frame) + OppositeWidthRate(10), 0, self.accessoryView.x - CGRectGetMaxX(self.headerImage.frame) - OppositeWidthRate(10) - OppositeWidthRate(10), self.height);
    self.backView.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
